<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\index\controller;
use mip\Init;
use think\Request;
use think\Response;
use think\Session;
use mip\Mip;
use com\Network;

class Spider extends Init
{

    private $pageSize = 2000;
    
    public function robots_txt(){
        $txt="User-agent: *
Disallow: /?s=/admin/*
Disallow: /admin/*
Crawl-delay: 300

sitemap:{$this->mipInfo['domain']}/sitemap.xml
";
        return Response::create($txt)->contentType('text/plain');;
    }

    function sitemap(){

        $sitemap = '<?xml version="1.0" encoding="utf-8"?>';
        $sitemap .= '<sitemapindex>';

        // 主菜单链接
        $mainmenuEditTime = db('mainMenu')->order('edit_time desc')->limit(1)->field('edit_time')->find();
        $sitemap .= '<sitemap>';
        $sitemap .= '<loc>' . $this->mipInfo['domain'] . '/mainmenu.xml</loc>';
        $sitemap .= '<lastmod>' .  $mainmenuEditTime['edit_time'] . '</lastmod>';
        $sitemap .= '</sitemap>';

        $count = model('app\article\model\Article')->getCount(0);
        $pageNum = ceil($count / $this->pageSize)+1;
        for ($i = 1; $i < $pageNum; $i++) {
            $sitemap .= '<sitemap>';
            $sitemap .= '<loc>' .  $this->mipInfo['domain'] . '/xml/' . $i . '.xml' . '</loc>';
            $sitemap .= '<lastmod>' . date("Y-m-d") . '</lastmod>';
            $sitemap .= '</sitemap>';
        }

        $sitemap .= '</sitemapindex>';
        return Response::create($sitemap)->contentType('text/xml');;
    }

    function xml()
    {
        $page = input('param.page');
        $page = $page ? $page : 1;
        $itemList = model('app\article\model\Article')->getList(array('status'=>1,'publish_time'=>array('lt','now')),'cid asc,sort desc', $page,1000);

        $xml = '<?xml version="1.0" encoding="utf-8"?>';
        $xml .= '<urlset>';
        if ($page == 1) {
            $xml .= '<url>';
            $xml .= '<loc>' . $this->mipInfo['domain'] . '/' . '</loc>';
            $xml .= '<lastmod>' . date("Y-m-d") . '</lastmod>';
            $xml .= '<changefreq>daily</changefreq>';
            $xml .= '<priority>1.0</priority>';
            $xml .= '</url>';
            if ($this->itemCategoryList = model('app\article\model\ArticleCategory')->getListByParent(0)) {
                foreach ($this->itemCategoryList as $k => $v) {
                    $xml .= '<url>';
                    $xml .= '<loc>' . $this->mipInfo['domain'].$v["url"] . '</loc>';
                    $xml .= '<lastmod>' . date("Y-m-d") . '</lastmod>';
                    $xml .= '<changefreq>daily</changefreq>';
                    $xml .= '<priority>0.9</priority>';
                    $xml .= '</url>';
                }
            }
        }
        foreach ($itemList as $k => $v) {
            $xml .= '<url>';
            $xml .= '<loc>' . $this->mipInfo['domain'].$v["url"] . '</loc>';
            $xml .= '<lastmod>' .date('Y-m-d',$v["ts_publish_time"]) . '</lastmod>';
            $xml .= '<changefreq>daily</changefreq>';
            $xml .= '<priority>0.6</priority>';
            $xml .= '</url>';
        }
        $xml .= '</urlset>';
        return Response::create($xml)->contentType('text/xml');;
    }


    function tagXml()
    {
        $page = input('param.id');
        $page = $page ? $page : 1;
        $xml = '<?xml version="1.0" encoding="utf-8"?>';
        $xml .= '<urlset>';
        $tagsList = db('tags')->page($page, $this->pageSize)->select();
        if ($tagsList) {
            foreach ($tagsList as $key => $val) {
                if ($val['url_name']) {
                    $tagsList[$key]['url'] = $this->mipInfo['domain'] . '/tag/' . $val['url_name'] . '/';
                } else {
                    $tagsList[$key]['url'] = $this->mipInfo['domain'] . '/tag/' . $val['id'] . '/';
                }
                $tagsList[$key]['time'] = $val['add_time'] ? date("Y-m-d", $val["add_time"]) : date("Y-m-d");
            }
            foreach ($tagsList as $key => $val) {
                $xml .= '<url>';
                $xml .= '<loc>' . $val["url"] . '</loc>';
                $xml .= '<lastmod>' . $tagsList[$key]['time'] . '</lastmod>';
                $xml .= '<changefreq>daily</changefreq>';
                $xml .= '<priority>0.9</priority>';
                $xml .= '</url>';
            }
        }
        $xml .= '</urlset>';
        return Response::create($xml)->contentType('text/xml');;
    }

    function baiduSitemapPc()
    {
        $count = model('app\article\model\Article')->getCount();
        $pageNum = ceil($count / $this->pageSize) + 1;
        $sitemap = '<?xml version="1.0" encoding="utf-8"?>';
        $sitemap .= '<sitemapindex>';
        for ($i = 1; $i < $pageNum; $i++) {
            $sitemap .= '<sitemap>';
            $sitemap .= '<loc>' . $this->mipInfo['domain'] . '/pcXml/' . $i . '.xml' . '</loc>';
            $sitemap .= '<lastmod>' . date("Y-m-d") . '</lastmod>';
            $sitemap .= '</sitemap>';
        }
        $sitemap .= '</sitemapindex>';
        return Response::create($sitemap)->contentType('text/xml');;
    }

    function pcXml()
    {
        $page = input('param.id');
        $page = $page ? $page : 1;
        $itemList = model('app\article\model\Article')->getList('',null, 'cid asc,sort desc',$page,'2000');

        $xml = '<?xml version="1.0" encoding="utf-8"?>';
        $xml .= '<urlset>';
        foreach ($itemList as $k => $v) {
            $xml .= '<url>';
            $xml .= '<loc>' . $v["url"] . '</loc>';
            $xml .= '<lastmod>' .date('Y-m-d', $v["ts_publish_time"]) . '</lastmod>';
            $xml .= '<changefreq>daily</changefreq>';
            $xml .= '<priority>0.6</priority>';
            $xml .= '<data>';
            $xml .= '<display>';
            $xml .= '<title>' . $v['title'] . '</title>';
            $xml .= '</display>';
            $xml .= '</data>';
            $xml .= '</url>';
        }
        $xml .= '</urlset>';
        return Response::create($xml)->contentType('text/xml');;
    }

    function mainmenu()
    {
        $xml = '<?xml version="1.0" encoding="utf-8"?>';
        $xml .= '<urlset>';
        $items = model('app\setting\model\Mainmenu')->getList();
        if ($items) {
            foreach ($items as $key => $val) {

                $xml .= '<url>';
                $xml .= '<loc>' . config('mipInfo')['domain']. $val['item_url'] . '</loc>';
                $xml .= '<lastmod>' . $val['edit_time'] . '</lastmod>';
                $xml .= '<changefreq>daily</changefreq>';
                $xml .= '<priority>1</priority>';
                $xml .= '</url>';
            }
        }
        $xml .= '</urlset>';
        return Response::create($xml)->contentType('text/xml');
    }


}
